package com.toivan.mt.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hwangjr.rxbus.RxBus;
import com.toivan.mt.R;
import com.toivan.mt.model.RxBusAction;

import java.util.List;

public class MtCuteClassAdapter extends RecyclerView.Adapter<MtCuteClassViewHolder> {
    private final List<Integer> list;

    public MtCuteClassAdapter(List<Integer> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MtCuteClassViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new MtCuteClassViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mt_cute, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final MtCuteClassViewHolder holder, int position) {
        if (position == 0) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
            p.setMargins((int) (holder.itemView.getContext().getResources().getDisplayMetrics().density * 14 + 0.5f), 0, 0, 0);
            holder.itemView.requestLayout();
        } else {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
            p.setMargins(0, 0, 0, 0);
            holder.itemView.requestLayout();
        }
        holder.imageIV.setImageResource(list.get(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.get().post(RxBusAction.ACTION_CUTE_CLASS_SELECTED, holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }



}
